<!---
Copyright (c) 2007 Brian Billman

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
--->
<cfcomponent displayname="CFSmugAuth" output="false" extends="CFSmugBase">

	<cffunction name="anonymously" access="public" output="false" returntype="string" hint="This method establishes a session which isn't logged in. Most of the other methods require a SessionID, which is returned by this method. Store temporarily and re-use it for all other methods which require SessionID.">
		<cfset var response = 0 />

		<cfset response = doRemoteInvocation( method = "smugmug.login.anonymously" ) />

		<cfreturn convertResponse( response ) />
	</cffunction>

	<cffunction name="withPassword" access="public" output="false" returntype="struct" hint="This method establishes a session and logs a user in based on the specified email address and password. Most of the other methods require a SessionID, which is returned by this method. Store temporarily and re-use it for all other methods which require SessionID.">
		<cfargument name="EmailAddress" type="string" required="true" />
		<cfargument name="Password" type="string" required="true" />

		<cfset var response = 0 />
		<cfset var argsCopy = structNew() />

		<cfset argsCopy = createSmugMugArgs( functionMetadata = getMetaData( this.withPassword ), argsPassed = Arguments ) />

		<cfset response = doRemoteInvocation( method = "smugmug.login.withPassword",
											args = argsCopy,
										 	secureMode = true ) />

		<cfreturn convertResponse( response ) />
	</cffunction>

	<cffunction name="withHash" access="public" output="false" returntype="struct" hint="This method establishes a session and logs in with a stored hash, rather than the real password. Slightly more secure than logging in with a plaintext password, it's still recommended that this step occur via SSL whenever possible. Most of the other methods require a SessionID, which is returned by this method. Store temporarily and re-use it for all other methods which require SessionID.">
		<cfargument name="UserID" type="numeric" required="true" />
		<cfargument name="PasswordHash" type="string" required="true" />

		<cfset var response = 0 />
		<cfset var argsCopy = structNew() />

		<cfset argsCopy = createSmugMugArgs( functionMetadata = getMetaData( this.withHash ), argsPassed = Arguments ) />

		<cfset response = doRemoteInvocation( method = "smugmug.login.withHash",
											args = argsCopy,
											secureMode = true ) />

		<cfreturn convertResponse( response ) />
	</cffunction>

	<cffunction name="logout" access="public" output="false" returntype="void" hint="This method logs a session out.">
		<cfargument name="SessionID" type="string" required="true" />

		<cfset var argsCopy = structNew() />

		<cfset argsCopy = createSmugMugArgs( functionMetadata = getMetaData( this.logout ), argsPassed = Arguments ) />

		<cfset doRemoteInvocation( method = "smugmug.logout",
									args = argsCopy,
								    secureMode = true ) />

		<cfreturn />
	</cffunction>

	<cffunction name="convertResponse" access="private" output="false" returntype="any">
		<cfargument name="responseXML" type="xml" required="true" />
		<cfset var x = 0 />
		<cfset var s = 0 />

		<cftry>

			<cfif validateRESTResponse( Arguments.responseXML )>
				<cfset x = Arguments.responseXML.rsp />
			</cfif>

			<cfswitch expression="#LCase( x.method.XmlText )#">

				<cfcase value="smugmug.login.anonymously">
					<!---
						String "SessionID"
					 --->
					<cfset s = x.Login.Session.XmlAttributes.id />

				</cfcase>

				<cfcase value="smugmug.login.withpassword">
					<!---
						String "SessionID"
						String "UserID"?
						String "NickName"?
						String "DisplayName"
						String "PasswordHash"
						String "AccountType"
						int "FileSizeLimit"
					--->

					<cfset s = StructNew() />
					<cfset s["SessionID"] = x.Login.Session.XmlAttributes.id />
					<cfset s["UserID"] = x.Login.User.XmlAttributes.id />
					<cfset s["NickName"] = x.Login.User.XmlAttributes.NickName />
					<cfset s["DisplayName"] = x.Login.User.XmlAttributes.DisplayName />
					<cfset s["PasswordHash"] = x.Login.XmlAttributes.PasswordHash />
					<cfset s["AccountType"] = x.Login.XmlAttributes.AccountType />
					<cfset s["FileSizeLimit"] = x.Login.XmlAttributes.FileSizeLimit />

				</cfcase>

				<cfcase value="smugmug.login.withhash">
					<!---
						String "SessionID"?
						String "NickName"?
						String "DisplayName"
						String "AccountType"
						int "FileSizeLimit"
					--->

					<cfset s = StructNew() />
					<cfset s["SessionID"] = x.Login.Session.XmlAttributes.id />
					<cfset s["NickName"] = x.Login.User.XmlAttributes.NickName />
					<cfset s["DisplayName"] = x.Login.User.XmlAttributes.DisplayName />
					<cfset s["AccountType"] = x.Login.XmlAttributes.AccountType />
					<cfset s["FileSizeLimit"] = x.Login.XmlAttributes.AccountType />

				</cfcase>

				<cfdefaultcase>
					<cfset logAction( file="CFSmug.SmugMugAuth.convertResponse", text="Message = Received response from unexpected method: '#x.method.XmlText#'", type="Warning" ) />
					<cfthrow type="CFSmug.SmugMugAuth.convertResponse" message="Received response from unexpected method: '#x.method.XmlText#'" />
				</cfdefaultcase>

			</cfswitch>

			<cfreturn s />


			<cfcatch type="CFSmug">
				<cfrethrow />
			</cfcatch>
			<cfcatch type="any">
				<cfset logAction( file="CFSmug.CFSmugAuth.convertResponse", text="Error converting response; Message=#cfcatch.message#; Detail=#cfcatch.detail#; Stacktrace=#cfcatch.stacktrace#", type="Error" ) />
				<cfthrow type="CFSmug.CFSmugAuth.convertResponse"
						message="Error converting response" detail="Message: #cfcatch.message#; Detail: #cfcatch.detail#" />
			</cfcatch>
		</cftry>
	</cffunction>

</cfcomponent>